#include <fstream>
#include <iosfwd>

#include "wcfactory.h"
#include "txtwc.h"
#include "xmlwc.h"


WordCounterFactory::WordCounterFactory(std::string const & file) : wc_(0)
{
    size_t pos = file.rfind('.');
    if(pos != std::string::npos)
    {
        std::ifstream in(file.c_str());
        if(in != 0)
        {
            in.close();

            std::string suffix = file.substr(pos, std::string::npos);
            if(suffix == ".txt")
                wc_ = new TxtWordCounter(file);
            else if(suffix == ".xml")
                wc_ = new XmlWordCounter(file);
        }
    }

}

WordCounter * WordCounterFactory::get() const
{
    return wc_;
}

WordCounterFactory::~WordCounterFactory()
{
    delete wc_;
}
